﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ECWindows
{
    public partial class Form_hello : Form
    {
        DateLogic.ClientLogic m_CL;
        DataPipe.DataPipe m_DP;
        string m_CerFile;
        public Form_hello()
        {
            m_DP = new DataPipe.DataPipe();
            m_CL = new DateLogic.ClientLogic();
            m_CL.LinkPipe(m_DP);
            InitializeComponent();
        }

        private void button_login_Click(object sender, EventArgs e)
        {
            if (textBox_password.Text == "")
            {
                MessageBox.Show("请输入正确密码！", "Warning!", MessageBoxButtons.OK);
            }
            if (textBox_password.Text != "")
            {
                byte[] baFilename = new byte[255];
                Array.Copy(Global.SafeBC.GetBytes(m_CerFile), baFilename, Global.SafeBC.GetBytes(m_CerFile).Length);
                byte[] baSend = Global.SafeBC.LinkBytes(baFilename, Global.SafeBC.GetBytes(textBox_password.Text));
                byte[] bRecv = m_CL.ClientOP(DateLogic.ClientOperation.Login, baSend, baSend.Length);
                if (bRecv[0] == (byte)DateLogic.ClientOperation.Login && bRecv[1] == (byte)DateLogic.ServerReturn.Success)
                {
                    Form_welcome f = new Form_welcome(m_CL);
                    f.Show();
                    this.Close();
                    return;
                }
                if (bRecv[1] == (byte)DateLogic.ServerReturn.InvalidCertificate)
                {
                    MessageBox.Show("Invalid Certificate or Invalid Login Code");
                }
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button_upload_Click(object sender, EventArgs e)
        {
            OpenFileDialog opencertificate = new OpenFileDialog();
            DialogResult dr = opencertificate.ShowDialog();
            if (dr == DialogResult.OK)
            {
                textBox1.Text = m_CerFile;
                m_CerFile = opencertificate.FileName;
            }
        }
    }
}
